package com.ereport.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Accounts;
import com.ereport.entity.Projects;
import com.ereport.util.ApplicationContext;

public class ProjectsDAO {
static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(Projects projects) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(projects);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<Projects> getAllProj(int id) throws Exception {
		List<Projects> aeb=new ArrayList<Projects>();
		Session session=startOperations();
		Query qry=session.createQuery("from Projects where exe_id=:id ");
		qry.setInteger("id", id);
		aeb=qry.list();
		endOperations(session);
		return aeb;
		
	}

	public Projects testProj(int id, String projName) throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from Projects where exe_id=:id and proj_name=:projName");
		qry.setInteger("id", id);
		qry.setString("projName", projName);
		Projects prj=(Projects)qry.uniqueResult();
		endOperations(session);
		return prj;
	}

	public Projects getProj(int id, int prid) throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from Projects where exe_id=:id and id=:prid");
		qry.setInteger("id", id);
		qry.setInteger("prid", prid);
		Projects prj=(Projects)qry.uniqueResult();
		endOperations(session);
		return prj;
	}

	public List<Projects> findAll() throws Exception {
		List<Projects> aeb=new ArrayList<Projects>();
		Session session=startOperations();
		Query qry=session.createQuery("from Projects");
		aeb=qry.list();
		endOperations(session);
		return aeb;
	}

	public List<Projects> findByExeId(int exid) throws Exception {
		List<Projects> aeb=new ArrayList<Projects>();
		Session session=startOperations();
		Query qry=null;
		if(exid==0){
			 qry=session.createQuery("from Projects");
		}else{
			 qry=session.createQuery("from Projects where exe_id=:exeid");
			 qry.setInteger("exeid",exid);
		}
		
		
		aeb=qry.list();
		endOperations(session);
		return aeb;
	}

	
	
}
